#include "addgoods.h"
#include "ui_addgoods.h"

addgoods::addgoods(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addgoods)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    this->setFixedSize(this->width(),this->height());
    ui->lineEdit_goodsid->setFocus();
}

addgoods::~addgoods()
{
    delete ui;
}

void addgoods::on_pushButton_add_clicked()
{
    QString goodsid = ui->lineEdit_goodsid->text();
        QString goodname = ui->lineEdit_goodsname->text();
        QString goodnumber = ui->lineEdit_goodsnumber->text();
        QString goodprice = ui->lineEdit_goodsprice->text();
        QString director = ui->lineEdit_director->text();
        QString supplier = ui->lineEdit_supplier->text();
        QString remarks = ui->plainTextEdit_remarks->toPlainText();

        QDateTime currentTime = QDateTime::currentDateTime();
        QString inputtime = currentTime.toString("yyyy-MM-dd hh:mm:ss");
        QString outputtime = currentTime.toString("yyyy-MM-dd hh:mm:ss");
        if(ui->lineEdit_goodsid->text().isEmpty())
        {
            QMessageBox::critical(this,"Tips","商品编号不能为空！",QMessageBox::Yes);
            ui->lineEdit_goodsid->setFocus();
            return;

        }

        QSqlQuery sql;
        sql.prepare("INSERT INTO stock_info(stockid, stockname, stockamount, stockunitprice, supplier, director, warehoustime,deliverytime,remarks) "
                    "VALUES (:goodsid, :goodname, :goodnumber, :goodprice, :supplier, :director, :time,:outtime, :remarks)");
        sql.bindValue(":goodsid", goodsid.toInt());
        sql.bindValue(":goodname", goodname);
        sql.bindValue(":goodnumber", goodnumber);
        sql.bindValue(":goodprice", goodprice);
        sql.bindValue(":supplier", supplier);
        sql.bindValue(":director", director);
        sql.bindValue(":time", inputtime);
        sql.bindValue(":outtime", outputtime);
        sql.bindValue(":remarks", remarks);
        if(sql.exec())
        {
            QMessageBox::information(this,"提示","插入成功！！",QMessageBox::Yes);
        }
        else
        {
            sql.prepare("SELECT stockid FROM stock_info WHERE stockid=:goodsid");
            sql.bindValue(":goodsid",goodsid);

            if(sql.exec() && sql.next())  // 执行查询并将记录移到第一条
                {
                    QString id = sql.value(0).toString();
                    QMessageBox::information(this,"提示","该商品编号已存在",QMessageBox::Yes);
                }


        }

}

void addgoods::on_pushButton_cancel_clicked()
{
    this->close();
}
